package com.rran.study.chat.friend.service.impl;

import com.alibaba.fastjson.JSON;
import com.rran.study.chat.common.model.Response;
import com.rran.study.chat.common.utils.BeanCopyUtil;
import com.rran.study.chat.common.utils.RedisUtil;
import com.rran.study.chat.friend.dto.request.AcceptFriendDTO;
import com.rran.study.chat.friend.dto.request.AddFriendDTO;
import com.rran.study.chat.friend.dto.response.QueryFriendAppListVO;
import com.rran.study.chat.friend.dto.response.QueryFriendsListVO;
import com.rran.study.chat.friend.dto.response.UserInfoVO;
import com.rran.study.chat.friend.entity.FriendApplicationDO;
import com.rran.study.chat.friend.entity.QueryFriendAppListDO;
import com.rran.study.chat.friend.entity.UserInfoDO;
import com.rran.study.chat.friend.mapper.FriendApplicationMapper;
import com.rran.study.chat.friend.mapper.UserMapper;
import com.rran.study.chat.friend.service.FriendService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

/**
 * @author yy
 * @Type FriendServiceImpl.java
 * @Desc
 * @date 2020/8/11 11:50
 */
@Service
@Slf4j
public class FriendServiceImpl implements FriendService {

    @Autowired
    private RedisUtil redisUtil;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private FriendApplicationMapper friendApplicationMapper;

    @Override
    public Response<QueryFriendsListVO> queryFriendsList(String beanId) {
        String key = beanId + ":" + "friends";
        Set<String> friendBeanIds = redisUtil.setMembers(key);
        if (friendBeanIds.size() == 0) {
            QueryFriendsListVO result = new QueryFriendsListVO();
            result.setFriendList(new ArrayList<>());
            return new Response<>(result);
        }
        List<UserInfoDO> userList = userMapper.queryBaseUserInfoList(friendBeanIds);
        List<UserInfoVO> list = BeanCopyUtil.copyListFromSrcToDest(userList, UserInfoVO.class);
        QueryFriendsListVO result = new QueryFriendsListVO();
        result.setFriendList(list);
        return new Response<>(result);
    }

    @Override
    public Response addFriend(AddFriendDTO addFriendDTO) {
        FriendApplicationDO friendApplicationDO = new FriendApplicationDO();
        BeanCopyUtil.copyFromSrcToDest(addFriendDTO, friendApplicationDO);
        //查询重复记录
        Integer count = friendApplicationMapper.querySingleFriendApp(friendApplicationDO);
        if (count > 0) {
            log.info("重复申请:{}", JSON.toJSONString(addFriendDTO));
            return Response.success();
        }
        friendApplicationMapper.insert(friendApplicationDO);
        return new Response(friendApplicationDO.getId());
    }

    @Override
    public Response<Integer> acceptFriend(AcceptFriendDTO acceptFriendDTO) {
        //2.修改好友申请记录状态
        Integer result = friendApplicationMapper.updateState(acceptFriendDTO.getId(), acceptFriendDTO.getState());
        return Response.success(result);
    }

    @Override
    public Response<List<QueryFriendAppListVO>> queryFriendAppList(String beanId) {
        List<QueryFriendAppListDO> list = friendApplicationMapper.queryFriendAppByTarget(beanId);
        if(CollectionUtils.isEmpty(list)){
            return new Response<>(new ArrayList<>());
        }
        List<QueryFriendAppListVO> result = BeanCopyUtil.copyListFromSrcToDest(list, QueryFriendAppListVO.class);
        return new Response<>(result);
    }
}
